import type { ModuleId } from '../types'
import type { ModuleVersion } from './types'
import { ToolKit } from '../../../lib/utils/toolkit'
import { AppModule } from '../types'

export const APPEARANCE_MODULE_VERSIONS: Record<ModuleId, ModuleVersion[]> = {
  [AppModule.ThemeInherit.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.ThemeSync.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-11',
    changelog: '初始版本',
  }],
  [AppModule.ThemeStyle.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-11',
    changelog: '初始版本',
  }],
  [AppModule.AdEngine.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.TocEngine.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.StyleEnhancement.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.ElementHider.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.BeautifyLink.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-05',
    changelog: '初始版本',
  }],
  [AppModule.HideSearchHistory.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-22',
    changelog: '初始版本',
  }],
  [AppModule.HideSearchSuggestions.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-22',
    changelog: '初始版本',
  }],
  [AppModule.HideSearchHotLists.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-22',
    changelog: '初始版本',
  }],
  [AppModule.FileIcons.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-16',
    changelog: '初始版本',
  }],
  [AppModule.CommitIcons.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-16',
    changelog: '初始版本',
  }],
  [AppModule.DirTimeSimplifier.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-16',
    changelog: '初始版本',
  }],
}

export const CONTENT_MODULE_VERSIONS: Record<ModuleId, ModuleVersion[]> = {
  [AppModule.CopyCopyright.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.CopyProtection.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.PayWallHandler.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
}

export const DATA_PRESENTATION_MODULE_VERSIONS: Record<ModuleId, ModuleVersion[]> = {
  [AppModule.FloorMarker.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.TimeInterval.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-06',
    changelog: '初始版本',
  }],
  [AppModule.DiscussionAnalytics.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-01',
    changelog: '初始版本',
  }],
  [AppModule.UserAnalytics.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-06',
    changelog: '初始版本',
  }],
}

export const INTERACTION_MODULE_VERSIONS: Record<ModuleId, ModuleVersion[]> = {
  [AppModule.LinkDirect.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.BlockLogin.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.QuickScroll.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.StealthSearch.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.NewTabLinks.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.AutoPagination.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-09',
    changelog: '初始版本',
  }],
  [AppModule.MarkRead.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-14',
    changelog: '初始版本',
  }],
  [AppModule.SuperSearch.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-17',
    changelog: '初始版本',
  }],
  [AppModule.SvgCopy.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-20',
    changelog: '初始版本',
  }],
  [AppModule.KeyboardPagination.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-20',
    changelog: '初始版本',
  }],
  [AppModule.CleanSearchHistory.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-22',
    changelog: '初始版本',
  }],
  [AppModule.ConversationFlow.id]: [{
    version: '1.0.0',
    releaseDate: '2025-10-31',
    changelog: '初始版本',
  }],
  [AppModule.QuickDownload.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-05',
    changelog: '初始版本',
  }],
  [AppModule.ImageDetail.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-05',
    changelog: '初始版本',
  }],
  [AppModule.ImagePreview.id]: [{
    version: '1.0.0',
    releaseDate: '2025-11-17',
    changelog: '初始版本',
  }],
}

export const NAVIGATION_MODULE_VERSIONS: Record<ModuleId, ModuleVersion[]> = {
  [AppModule.NavbarMenu.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.AnswerNavigator.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkScriptNavigator.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
}

export const GREASY_FORK_MODULE_VERSIONS: Record<ModuleId, ModuleVersion[]> = {
  [AppModule.GreasyForkScriptValuation.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkScriptInstalls.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkScriptMark.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkFeedbackMark.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkFeedbackGroup.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkTimeHelper.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkSearchNavigator.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
  [AppModule.GreasyForkStatsPanel.id]: [{
    version: '1.0.0',
    releaseDate: '2025-09-01',
    changelog: '初始版本',
  }],
}

/**
 * 获取模块的所有版本信息
 * @param moduleId - 模块ID
 * @returns 版本信息数组或null
 */
export function getModuleVersions(moduleId: string): ModuleVersion[] | null {
  const allModuleVersions = {
    ...APPEARANCE_MODULE_VERSIONS,
    ...CONTENT_MODULE_VERSIONS,
    ...DATA_PRESENTATION_MODULE_VERSIONS,
    ...INTERACTION_MODULE_VERSIONS,
    ...NAVIGATION_MODULE_VERSIONS,
    ...GREASY_FORK_MODULE_VERSIONS,
  }

  const moduleVersions = allModuleVersions[moduleId]

  if (!moduleVersions)
    return null

  return moduleVersions
}

/**
 * 获取模块的最新版本信息
 * @param moduleId - 模块ID
 * @returns 最新版本信息或null
 */
export function getModuleLatestVersion(moduleId: string): ModuleVersion | null {
  const versions = getModuleVersions(moduleId)

  if (!versions || versions.length === 0)
    return null

  // 按版本号降序排序
  const sorted = [...versions].sort((a, b) => ToolKit.string.compareVersions(b.version, a.version))
  return sorted[0]
}
